﻿using System;
using System.Diagnostics;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using HYCodeBase.WinForm;
using HYElectricDim;
using YRevitJig;
using YJKRGeometry;
using HYView;

namespace ElectricDim
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdCableTypeDim : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			if (!(document.ActiveView is ViewPlan))
			{
				Result result = SwitchView.SetUsableView((CmdViewType)3, commandData);
				if (result != Result.Succeeded)
				{
					return result;
				}
			}
			ElectricalUtils.ViewValidating(document);
			WindowHandle windowHandle = null;
			if (windowHandle == null)
			{
				windowHandle = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			DlgCableTrayTypeDim instance = DlgCableTrayTypeDim.GetInstance(commandData.Application, windowHandle);
			instance.Show(windowHandle);
			double num = (double)document.ActiveView.Scale;
			new SubTransaction(activeUIDocument.Document);
			for (;;)
			{
				Transaction transaction = new Transaction(document, "LayLabel");
				try
				{
					transaction.Start();
					Reference reference = null;
					Element element = null;
					string markTypeName = instance.GetMarkTypeName();
					Autodesk.Revit.DB.ElementType elementType;
					try
					{
						if (markTypeName.Equals("竖直桥架规格标记"))
						{
							reference = activeUIDocument.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new RiserCableTrayFilter(), "请选择要标注的竖直桥架");
							element = document.GetElement(reference);
						}
						else
						{
							reference = activeUIDocument.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new CableTrayFilter(), "请选择要标注的桥架");
							element = document.GetElement(reference);
						}
						elementType = instance.GetElementType();
					}
					catch (Exception ex)
					{
						string message2 = ex.Message;
						if (instance.ManaulEsc)
						{
							instance.ManaulEsc = false;
							transaction.Commit();
							continue;
						}
						throw ex;
					}
					if (element == null)
					{
						transaction.RollBack();
					}
					else
					{
						try
						{
							ElementIdDate elementIdDate = Assist.ExtensibleStorageManager.ReadFromElement<ElementIdDate>(element, this.guid);
							if (elementIdDate.GetId().IntegerValue != 0)
							{
								document.Delete(elementIdDate.GetId());
							}
						}
						catch
						{
						}
						if (elementType == null)
						{
							MessageBox.Show("没有找到标记族。");
							transaction.RollBack();
							return Autodesk.Revit.UI.Result.Failed;
						}
						ElectricDimJig electricDimJig = new ElectricDimJig(activeUIDocument);
						if (instance.GetDimStyle() == LeaderStyle.Orthogonal)
						{
							electricDimJig.ResetPoint = XUiJigBase.EResetPoint.eByOrtho;
						}
						electricDimJig.RectWidth = AssistFunc.mmToFeet(38.0) * num;
						electricDimJig.RectHeight = AssistFunc.mmToFeet(10.0) * num;
						XYZ xyz = electricDimJig.PickDimPoint(reference.GlobalPoint, ElectricDimJig.EDimType.eSideBorder, "请拾取标注点:");
						electricDimJig.Dispose();
						if (xyz != null && xyz.DistanceTo(reference.GlobalPoint) >= 0.005)
						{
							IndependentTag independentTag = RevitVersionFuncs.NewTag(document, document.ActiveView, new Reference(element), false, 0, 0, xyz);
							independentTag.ChangeTypeId(elementType.Id);
							LeaderStyle dimStyle = instance.GetDimStyle();
							if (dimStyle == LeaderStyle.HasLeader || dimStyle == LeaderStyle.Orthogonal)
							{
                                independentTag.LeaderEndCondition = (LeaderEndCondition)1;
								independentTag.HasLeader = true;
								independentTag.LeaderEnd = reference.GlobalPoint;
								independentTag.LeaderElbow = xyz;
								BoundingBoxXYZ boundingBoxXYZ = independentTag.get_BoundingBox(document.ActiveView);
								XYZ min = boundingBoxXYZ.Min;
								double length = (new XYZ(boundingBoxXYZ.Max.X, boundingBoxXYZ.Min.Y, boundingBoxXYZ.Min.Z) - min).GetLength();
								if (Geometry.LessThan(xyz.X, reference.GlobalPoint.X))
								{
									independentTag.TagHeadPosition = xyz - new XYZ(length / 2.0, 0.0, 0.0);
								}
								else
								{
									independentTag.TagHeadPosition = xyz + new XYZ(length / 2.0, 0.0, 0.0);
								}
							}
							transaction.Commit();
							ElementIdDate obj = new ElementIdDate(independentTag.Id);
							Assist.ExtensibleStorageManager.WriteToElement(element, this.guid, obj);
							continue;
						}
						transaction.RollBack();
					}
				}
				catch (Exception ex2)
				{
					string message3 = ex2.Message;
					instance.Close();
					transaction.RollBack();
					return Autodesk.Revit.UI.Result.Succeeded;
				}
				break;
			}
			instance.Close();
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private Guid guid = new Guid("356cda5a-e6c5-4c2f-a9ef-b1227376b8c8");
	}
}
